package com.gaga.metadata.record;

import cn.hutool.core.bean.BeanUtil;
import com.gaga.metadata.common.utils.StringUtils;
import com.gaga.metadata.entity.base.SqlRecord;
import com.gaga.metadata.entity.database.DataBaseTable;
import com.gaga.metadata.result.SqlRecordResult;

/**
 * @description: sql日志生产类
 * @author: liujia
 * @date: 2023-08-23 14:12
 */
public class SqlRecordFactory {
    public static void handler(String sql, boolean executeResult, String errorMsg, DataBaseTable table) {
        if (StringUtils.isBlank(sql) || table == null || table.getSqlRecord() == null || !table.getSqlRecord().isEnabled()) {
            return;
        }

        SqlRecord sqlRecord = table.getSqlRecord();
        SqlRecordResult sqlRecordResult = new SqlRecordResult();
        BeanUtil.copyProperties(sqlRecord, sqlRecordResult);
        sqlRecordResult.setSql(sql);
        sqlRecordResult.setSuccess(executeResult);
        if (!executeResult) {
            sqlRecordResult.error(errorMsg);
        }
        try {
            sqlRecord.getQueue().put(sqlRecordResult);
        } catch (InterruptedException ignored) {

        }
    }
}
